<template>
  <div class="app-container">
    <div>
      <el-row :gutter="10">
        <el-col :span="6" :xs="24" :lg="24">
          <div class="bgwhite plr20 ptb20 radius10 flex-col">
            <div class="justify-between w100p">
              <div class="clr3c4 bold ft26">新增</div>
              <div class="clr9fa align-center pointer" @click="back()">
                <el-icon>
                  <Back />
                </el-icon>
                返回上一頁
              </div>
            </div>
            <div class="mt40">
              <el-form :label-position="labelPosition" label-width="auto" :model="formLabelAlign" style="max-width: 600px">
                <el-form-item label="等級:">
                  <el-input v-model="formLabelAlign.levelid" style="width: 380px;" placeholder="請輸入等級" />
                </el-form-item>
                <el-form-item label="等級名稱:" required>
                  <el-input v-model="formLabelAlign.levelname" placeholder="請輸入等級名稱" />
                </el-form-item>
                <el-form-item label="消費次數:" required>
                  <el-input v-model="formLabelAlign.level_up" placeholder="請輸入消費次數" />
                </el-form-item>
                <el-form-item label="消費折扣:" required>
                  <el-input v-model="formLabelAlign.bili" placeholder="請輸入消費折扣" />
                </el-form-item>
                <el-form-item label="標識:" style="align-items:flex-start;font-weight:bold;">
                  <el-upload v-model:file-list="fileList" :action="$upUrl" list-type="picture-card" :headers="headers" name="images" :on-success="onSuccess" :on-remove="handleRemove">
                    <div class="uploader flex-cen" v-if="fileList.length == 0">
                      <el-icon size="60" style="color: #3a3f63;">
                        <Picture />
                      </el-icon>
                    </div>
                    <el-button class="btn2 ml20">選擇文件</el-button>
                  </el-upload>
                </el-form-item>
                <el-form-item label="背景图:" style="align-items:flex-start;font-weight:bold;">
                  <el-upload v-model:file-list="fileList2" :action="$upUrl" list-type="picture-card" :headers="headers" name="images" :on-success="onSuccess2" :on-remove="handleRemove2">
                    <div class="uploader flex-cen" v-if="fileList2.length == 0">
                      <el-icon size="60" style="color: #3a3f63;">
                        <Picture />
                      </el-icon>
                    </div>
                    <el-button class="btn2 ml20">選擇文件</el-button>
                  </el-upload>
                </el-form-item>
                <el-form-item label="説明:" required>
                  <el-input v-model="formLabelAlign.tips" placeholder="請輸入説明" />
                </el-form-item>
                <el-form-item label="權益:" required>
                  <el-input v-model="formLabelAlign.equity" placeholder="請輸入權益" />
                </el-form-item>
              </el-form>
              <div class="mt40 mb15 flex-cen ">
                <div class="align-center">
                  <el-button class="btn1" @click="reset()">重置</el-button>
                  <el-button class="btn2" @click="submit()">提交</el-button>
                </div>

              </div>
            </div>


          </div>

        </el-col>

      </el-row>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router';
import type { FormProps } from 'element-plus'
import { levelAdd } from '@/api/level.js'
import { baseURL, headers, formateDate } from '@/utils/utils'

const labelPosition = ref<FormProps['labelPosition']>('right')

const formLabelAlign = reactive({
  levelid: '',
  levelname: '',
  level_up: '',
  bili: '',
  tips: '',
  equity: ''
})
const fileList = ref([])
const fileList2 = ref([])
const onSuccess = (res) => {
  console.log('上传成功', res)
  let path = res.data[0].path
  let fullpath = `${baseURL}${path}`
  let file = [
    {
      name: 'levelLogo',
      url: fullpath
    }
  ]
  fileList.value = file

}
const onSuccess2 = (res) => {
  console.log('上传成功', res)
  let path = res.data[0].path
  let fullpath = `${baseURL}${path}`
  let file = [
    {
      name: 'levelLogo',
      url: fullpath
    }
  ]
  fileList2.value = file

}
const handleRemove: UploadProps['onRemove'] = (uploadFile, uploadFiles) => {
  console.log(uploadFile, uploadFiles)
  fileList.value = []
}

const handleRemove2: UploadProps['onRemove'] = (uploadFile, uploadFiles) => {
  console.log(uploadFile, uploadFiles)
  fileList2.value = []
}

//重置
const reset = () => {
  fileList.value = []
  fileList2.value = []

  formLabelAlign.levelid = ''
  formLabelAlign.levelname = ''
  formLabelAlign.level_up = ''
  formLabelAlign.bili = ''
  formLabelAlign.tips = ''
  formLabelAlign.equity = ''
}
// 提交
const submit = () => {
  levelAdd({
    levelid: formLabelAlign.levelid,
    levelname: formLabelAlign.levelname,
    level_up: formLabelAlign.level_up,
    bili: formLabelAlign.bili,
    thumb: fileList.value.length ? fileList.value[0].url : '',
    bj_image: fileList2.value.length ? fileList2.value[0].url : '',
    tips: formLabelAlign.tips,
    equity: formLabelAlign.equity
  }).then((res) => {
    console.log('添加等级', res)
    back()
  })
}
const router = useRouter()
const back = () => {
  router.back()
}
</script>
<style scoped >
:deep(.el-upload--picture-card) {
  background-color: transparent;
  border: none;
  width: auto;
}

:deep(.el-input) {
  box-shadow: none;
}
</style>